package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GweoT;
import cn.e4j.modular.crm.service.impl.Gwei009ServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 回款
 * @author mengyb
 * @Date 2019/08/09
 */
//所有涉及ERP都不取
@Controller
@RequestMapping("/crm/gwei009")
public class Gwei009Controller extends BaseController {

    @Autowired
    private Gwei009ServiceImpl gwei009Service;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转回款页面
     */
    @RequiresPermissions("crm:gwei009:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei009.html";
    }


    @RequiresPermissions("crm:gwei009:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser= ShiroKit.getUser();

        inDto.put("ent", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        inDto.put("USER_ID", shiroUser.getID());
        //根据行业分类授权 不根据设备组授权
//        if(shiroUser.getDEVICE_ID()!=null && shiroUser.getDEVICE_ID().size()>0){
//            inDto.put("HAS_DEVICE",true);
//        }else{
//            inDto.put("HAS_DEVICE",false);
//        }
        List<Dto> resultList = this.gwei009Service.getList(inDto,page);
        for (int i = 0; i < resultList.size(); i++) {
            inDto.put("BIZ_CODE", resultList.get(i).getString("GWEOPLANT"));
            if (inDto.getString("BIZ_CODE").equals("")) {
                resultList.get(i).put("AZP02","");
            } else {
                Dto result = null;
                try {
                    result = this.erpLogic.getERPService("getAzpFile",inDto);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch(Exception e){
                    e.printStackTrace();
                }
                if(result!=null){
                    resultList.get(i).put("AZP02", result.getString("AZP02"));
                }else{
                    resultList.get(i).put("AZP02","");
                }
            }
        }
        return new PageReData<>(page,resultList);
    }

    /**
     * 新增页面,获取默认值
     */
    @RequestMapping("/getAddDef")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData getAddDef() {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.put("GWEODOCNO","HKDS"+ DateUtils.formatDate(new Date(),"yyyyMMddSSSSS"));
        outDto.put("GWEODOCDATE", DateUtils.getDate("yyyy-MM-dd"));
        outDto.put("ORG_ID",shiroUser.getORG_ID());
        outDto.put("NAME_ORG",shiroUser.getSysOrgT().getNAME());
        outDto.put("USER_ID",shiroUser.getID());
        outDto.put("NAME_USER",shiroUser.getNAME());
        return ResponseData.success(outDto);
    }

    /**
     * 获取汇率
     */
    @ResponseBody
    @RequiresPermissions("crm:gwei009:init")
    @RequestMapping("/getRate")
    public ResponseData getRate(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = gwei009Service.getRate(inDto);
        return ResponseData.success(outDto);
    }

    @ResponseBody
    @RequiresPermissions("crm:gwei009:init")
    @RequestMapping("/getTreeData")
    public ResponseData getTreeData(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();

        inDto.put("IS_DEL", "0");
        inDto.put("TYPE","2");
        List<Dto> resultDto = this.gwei009Service.listOrg_1(inDto);
        for(int i = 0; i < resultDto.size(); i ++){
            inDto.put("BIZ_CODE",resultDto.get(i).getString("BIZ_CODE"));
            Dto result = null;
            try {
                result = this.erpLogic.getERPService("getAzpFile",inDto);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch(Exception e){
                e.printStackTrace();
            }
            if(result!=null){
                resultDto.get(i).put("AZP02",result.getString("AZP02"));
            }else{
                resultDto.get(i).put("AZP02","");
            }
        }
        return ResponseData.success(resultDto);
    }

    /**
     * 编辑页面,获取回显数据
     */
    @RequestMapping("/getData")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData getData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto inDto=dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        List<Dto> resultList = this.gwei009Service.list(inDto);
        Dto dto = resultList.get(0);
        if (dto.getString("GWECPLANT").equals("")) {
            dto.put("AZP02", "");
        } else {
            dto.put("BIZ_CODE", dto.getString("GWECPLANT"));
            dto.put("ERP_Link",inDto.getString("ERP_Link"));
            Dto result = null;
            try {
                result = this.erpLogic.getERPService("getAzpFile",inDto);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
            if(result!=null){
                dto.put("AZP02", result.getString("AZP02"));
            }else{
                dto.put("AZP02","");
            }
        }
        return ResponseData.success(dto);
    }

    /**
     * 编辑页面,获取回显单身数据 GweoData
     */
    @RequestMapping("/getGweoData")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData getGweoData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto=dtoWapper.getDto();
        List<Dto> resultDto = this.gwei009Service.listGweo(inDto);
        return ResponseData.success(resultDto);
    }

    /**
     * 订单号开窗 从ERP库取
     */
    @RequestMapping("/getOeaList")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData getOeaList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> list = this.gwei009Service.getOebPage(inDto, page);
        return new PageReData(page, list);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwei009:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweoT> gweoTList = inDto.getReqList(GweoT.class);
        this.gwei009Service.add(gweoTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwei009:edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweoT> gweoTList = inDto.getReqList(GweoT.class);
        this.gwei009Service.edit(gweoTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/delRow")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData delRow(GweoT gweoT) {
        this.gwei009Service.delRow(gweoT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delRows")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData delRows(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweoT> gweoTList = inDto.getReqList(GweoT.class);
        this.gwei009Service.delRows(gweoTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量审核数据
     */
    @RequestMapping("/confirm")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData confirm(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweoT> gweoTList = inDto.getReqList(GweoT.class);
        Dto dto=this.gwei009Service.confirm(gweoTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 批量取消审核数据
     */
    @RequestMapping("/unConfirm")
    @RequiresPermissions("crm:gwei009:init")
    @ResponseBody
    public ResponseData unConfirm(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GweoT> gweoTList = inDto.getReqList(GweoT.class);
        Dto dto=this.gwei009Service.unConfirm(gweoTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

}
